package com.events.testservice.rest.v1;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.Response;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import com.events.testservice.dao.SalesOrderDao;
import com.events.testservice.entity.ProductEntity;
import com.events.testservice.entity.SalesOrderEntity;
import com.events.testservice.rest.v1.dto.ProductDto;
import com.events.testservice.rest.v1.dto.SalesOrderDto;

/**
 * Sales Order resource implementations.
 * 
 * @author Ashish Sonawane.
 * 
 */
@Service
public class SalesOrderResourceImpl implements SalesOrderResource {

	@Autowired
	private SalesOrderDao salesOrderDao;

	@Override
	public Response createSalesOrder(SalesOrderDto sampleDto) {
		if (sampleDto == null) {
			return Response.status(HttpStatus.BAD_REQUEST.value()).build();
		}

		SalesOrderEntity salesOrderEntity = new SalesOrderEntity.Builder()
				.id(sampleDto.getSalesOrderId()).name(sampleDto.getOrdername())
				.product(mapDtoToEntity(sampleDto.getProduct())).build();

		salesOrderEntity = salesOrderDao.createSalesOrder(salesOrderEntity);
		return Response.status(HttpStatus.OK.value())
				.entity(salesOrderEntity.getSalesOrderId()).build();
	}

	@Override
	public Response getSalesOrder(Long id) {
		if (id == null) {
			return Response.status(HttpStatus.BAD_REQUEST.value()).build();
		}
		SalesOrderEntity entity = salesOrderDao.findSalesOrderById(id);
		if (entity == null) {
			return Response.status(HttpStatus.NO_CONTENT.value()).build();
		}
		SalesOrderDto dto = new SalesOrderDto.Builder()
				.id(entity.getSalesOrderId()).name(entity.getOrdername())
				.product(mapEntityToDto(entity.getProduct())).build();
		return Response.status(HttpStatus.OK.value()).entity(dto).build();
	}

	@Override
	public Response deleteSalesOrder(Long id) {

		if (id == null) {
			return Response.status(HttpStatus.BAD_REQUEST.value()).build();
		}
		salesOrderDao.deleteSalesOrderById(id);
		return null;
	}

	@Override
	public Response updateSalesOrder(Long id, String orderName) {
		if (id == null) {
			return Response.status(HttpStatus.BAD_REQUEST.value()).build();
		}
		SalesOrderEntity entity = salesOrderDao.updateOrderById(id, orderName);
		if (entity == null) {
			return Response.status(HttpStatus.NO_CONTENT.value()).build();
		}
		SalesOrderDto dto = new SalesOrderDto.Builder()
				.id(entity.getSalesOrderId()).name(entity.getOrdername())
				.product(mapEntityToDto(entity.getProduct())).build();
		return Response.status(HttpStatus.OK.value()).entity(dto).build();
	}

	private List<ProductEntity> mapDtoToEntity(List<ProductDto> dtos) {
		List<ProductEntity> entities = new ArrayList<ProductEntity>();
		for (ProductDto productDto : dtos) {
			ProductEntity productEntity = new ProductEntity();
			productEntity.setId(productDto.getId());
			productEntity.setName(productDto.getName());
			productEntity.setPrice(productDto.getPrice());
			entities.add(productEntity);
		}
		return entities;
	}

	private List<ProductDto> mapEntityToDto(List<ProductEntity> entities) {
		List<ProductDto> productDtos = new ArrayList<ProductDto>();
		for (ProductEntity productEntity : entities) {
			ProductDto productDto = new ProductDto();
			productDto.setId(productEntity.getId());
			productDto.setName(productEntity.getName());
			productDto.setPrice(productEntity.getPrice());
			productDtos.add(productDto);
		}
		return productDtos;
	}
}
